<template>
  <BasicLayout>
    <template #wrapper>
      <el-card class="box-card">

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-permisaction="['platform:recommend_course:add']"
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增
            </el-button>
          </el-col>

          <el-col :span="1.5">
            <el-button
              v-permisaction="['platform:recommend_course:remove']"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              >删除
            </el-button>
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="recommendcourseList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />

          <el-table-column label="课程封面">
            <template slot-scope="scope"
              ><el-avatar
                shape="square"
                :size="100"
                :src="scope.row.courseInfo.cover"
              ></el-avatar>
            </template>
          </el-table-column>

          <el-table-column prop="courseInfo.title" label="课时标题">
          </el-table-column>
          <el-table-column prop="lessonList" label="推荐课时">
            <template slot-scope="scope">
              <div v-for="(item, index) in scope.row.lessonList" :key="index">
                <el-card class="box-card">
                  <el-avatar :size="40" :src="item.mediaCover"></el-avatar>
                  {{ item.title }}
                </el-card>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">

              <el-button
                v-permisaction="['platform:recommend_course:remove']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改对话框 -->
        <el-dialog
          :title="title"
          :visible.sync="open"
          width="900px"
          style="align-items: flex-start"
        >
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="课程" prop="courseId">
              <CourseList v-on:setCourseId="setCourseId" />
            </el-form-item>

            <el-form-item label="课程课时">
              <el-table
                ref="multipleTable"
                :data="lessonList"
                tooltip-effect="dark"
                border
                style="width: 100%"
                @selection-change="handleLessonSelectionChange"
              >
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column label="课时封面">
                  <template slot-scope="scope"
                    ><el-avatar
                      shape="square"
                      :size="100"
                      :src="scope.row.mediaCover"
                    ></el-avatar>
                  </template>
                </el-table-column>
                <el-table-column prop="title" label="课时标题">
                </el-table-column>
              </el-table>
            </el-form-item>

            <el-form-item label="权重" prop="weigh">
              <el-input-number
                v-model="form.weigh"
                placeholder="权重"
              ></el-input-number>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-card>
    </template>
  </BasicLayout>
</template>

<script>
import {
  addRecommendCourse,
  delRecommendCourse,
  getRecommendCourse,
  listRecommendCourse,
  updateRecommendCourse,
} from "@/api/edu/platform/recommend_course";

import { lessonList } from "@/api/edu/service/index";
//课程选择器
import CourseList from "@/components/MyCourse/CourseList";

export default {
  name: "RecommendCourse",
  components: {
    CourseList,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isEdit: false,
      // 类型数据字典
      typeOptions: [],
      recommendcourseList: [],

      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        courseId: undefined,
        recommendCourseId: undefined,
      },
      // 表单参数
      form: {
        weigh: 1,
        chapterIdList: [],
      },
      // 表单校验
      rules: {
        courseId: [
          { required: true, message: "课程ID不能为空", trigger: "blur" },
        ],
        recommendCourseId: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
      },
      //课程的课时列表
      lessonList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listRecommendCourse(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((response) => {
        this.recommendcourseList = response.data.list;
        this.total = response.data.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: undefined,
        recommendCourseId: undefined,
        weigh: 1,
        chapterIdList: [],
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加推荐课程";
      this.isEdit = false;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.recommendCourseId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recommendCourseId = row.recommendCourseId || this.ids;
      getRecommendCourse(recommendCourseId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改推荐课程";
        this.isEdit = true;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.recommendCourseId !== undefined) {
            updateRecommendCourse(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addRecommendCourse(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.recommendCourseId || this.ids;
      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delRecommendCourse(Ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },

    //查询课程的课时列表
    getLessonList(courseId) {
      lessonList(courseId).then((res) => {
        this.lessonList = res.data.list;
      });
    },

    // handleLessonSelectionChange
    handleLessonSelectionChange(selection) {
      console.log(selection);
      this.form.chapterIdList = [];
      selection.forEach((item, index) => {
        var chapterItem = {};
        chapterItem["chapterId"] = item.chapterId;
        chapterItem["weigh"] = index + 1;
        this.form.chapterIdList[index] = chapterItem;
      });
    },
    //接收用户IDs
    setCourseId: function (courseId) {
      // childValue就是子组件传过来的值
      // console.log("userIds: "+userIds);
      this.form.courseId = courseId;
      console.log("this.form.courseId: " + this.form.courseId);
      this.getLessonList(this.form.courseId);
    },
  },
};
</script>
